#include "ModbusListXml.h"
#include "System/System.h"
#include "Port/UART.h"
ModbusListXml::ModbusListXml()
{
       m_queryNum=0;
       m_queryStata=0;
       m_queryNo=0;
}

bool ModbusListXml::parser()
{
    if(!this->check())
    {
        return false;
    }
    QFile file(m_path);
    file.open(QFile::ReadOnly | QFile::Text);
    QDomDocument doc;
    doc.setContent(&file);
    file.close();
    QDomElement root = doc.documentElement();
    QDomNode rootChild = root.firstChild();

    while (!rootChild.isNull())
    {
        if(rootChild.toElement().tagName() == "Modbus")   //解析场景点位
        {
            QDomNode modbusChild = rootChild.toElement().firstChild();
            quint8 deviceAddress = 0;
            while(!modbusChild.isNull())
            {
                if(modbusChild.toElement().tagName() == "DeviceAddress")
                {
                    deviceAddress = modbusChild.toElement().text().toUInt();
                    m_deviceAddressList.append(deviceAddress);
                }
                if(modbusChild.toElement().tagName() == "FunctionList")
                {
                    QDomNode functionListChild = modbusChild.toElement().firstChild();
                    while(!functionListChild.isNull())
                    {
                        if(functionListChild.toElement().tagName() == "Function")
                        {
                            QDomNode functionChild = functionListChild.toElement().firstChild();
                            quint8 functionCode = 0;
                            while(!functionChild.isNull())
                            {
                                if(functionChild.toElement().tagName() == "FunctionCode")
                                {
                                    functionCode = functionChild.toElement().text().toUInt();
                                }
                                if(functionChild.toElement().tagName() == "ActionList")
                                {
                                    QDomNode actionListChild = functionChild.toElement().firstChild();
                                    while(!actionListChild.isNull())
                                    {
                                        if(actionListChild.toElement().tagName() == "Action")
                                        {
                                            ModbusControlStruct  control;
                                            ControlPointStruct controlPoint;
                                            control.setDeviceAddress(deviceAddress);
                                            control.setFunctionCode(functionCode);
                                            QDomNode actionChild = actionListChild.toElement().firstChild();
                                            while(!actionChild.isNull())
                                            {
                                                if(actionChild.toElement().tagName() == "ControlPoint")
                                                {
                                                    QStringList strTemp = actionChild.toElement().text().split(",");
                                                    controlPoint.setAddress(strTemp.at(0).toInt());
                                                    control.setPointAddress(strTemp.at(0).toInt());
                                                    controlPoint.setValue(strTemp.at(1).toInt());
                                                }
                                                if(actionChild.toElement().tagName() == "CalculationMode")
                                                {
                                                   quint16 calculationMode = actionChild.toElement().text().toUInt();
                                                    control.setCalculationMode(calculationMode);
                                                }
                                                if(actionChild.toElement().tagName() == "QueryTime")
                                                {
                                                   quint16 setTime = actionChild.toElement().text().toUInt();
                                                    control.setqueryTime(setTime);
                                                    qDebug()<<"xml setTime="<<setTime;
                                                }
                                                if(actionChild.toElement().tagName() == "RegisterStartAddress")
                                                {
                                                    quint16 registerStartAddress = actionChild.toElement().text().toUInt();
                                                    control.setRegisterStartAddress(registerStartAddress);
                                                }
                                                if(actionChild.toElement().tagName() == "RegisterNumber")
                                                {
                                                    quint16 registerNumber = actionChild.toElement().text().toUInt();
                                                    control.setRegisterNumber(registerNumber);
                                                }
                                                if(actionChild.toElement().tagName() == "RegistrValueList")
                                                {
                                                    QStringList strRegistrValueList = actionChild.toElement().text().split(",");
                                                    QList<quint16> registrValueList;
                                                    foreach (const QString &strRegistrValue, strRegistrValueList)
                                                    {
                                                        registrValueList.append(strRegistrValue.toUInt());
                                                    }
                                                    control.setRegistrValueList(registrValueList);
                                                }
                                                if(actionChild.toElement().tagName() == "Transition")
                                                {
                                                    QDomNode transitionChild = actionChild.toElement().firstChild();
                                                    QList<RangePointStruct> transitionList;
                                                    while(!transitionChild.isNull())
                                                    {
                                                        if(transitionChild.toElement().tagName() == "Register")
                                                        {
                                                            RangePointStruct transition;
                                                            transition.setAddress(transitionChild.toElement().attribute("address").toInt());
                                                            QString strValue = transitionChild.toElement().attribute("value");
                                                            int valueMin,valueMax;
                                                            if(strValue.contains("-"))
                                                            {
                                                                QStringList strTemp = strValue.split("-");
                                                                valueMin = strTemp.at(0).toInt();
                                                                valueMax = strTemp.at(1).toInt();
                                                            }
                                                            else
                                                            {
                                                                valueMin = strValue.toInt();
                                                                valueMax = strValue.toInt();
                                                            }
                                                            transition.setValueMin(valueMin);
                                                            transition.setValueMax(valueMax);


                                                            QList<ControlPointStruct> transformPointList;
                                                            QDomNode transformPointChild = transitionChild.toElement().firstChild();
                                                            while(!transformPointChild.isNull())
                                                            {
                                                                if (transformPointChild.toElement().tagName() == "TransformPoint")  //解析控制点位
                                                                {
                                                                    ControlPointStruct transformPoint;
                                                                    QStringList strTemp = transformPointChild.toElement().text().split(",");
                                                                    transformPoint.setAddress(strTemp.at(0).toInt());
                                                                    transformPoint.setValue(strTemp.at(1).toInt());
                                                                    transformPointList.append(transformPoint);
                                                                }
                                                                transformPointChild = transformPointChild.nextSibling();
                                                            }
                                                            transition.setControlPointList(transformPointList);
                                                            transitionList.append(transition);
                                                        }
                                                        transitionChild = transitionChild.nextSibling();
                                                    }
                                                    m_transitionMap.insert(control,transitionList);
                                                }
                                                actionChild = actionChild.nextSibling();
                                            }
                                            m_modbusStructList.append(control);
                                            m_controlMultiMap.insert(controlPoint,control);
                                            m_pointMultiMap.insert(control,controlPoint);
                                            if(control.functionCode()==0x03)
                                            {
                                                m_modbusQueryMap.insert(m_queryNum,control);
                                                m_queryNum++;
                                            }
                                        }
                                        actionListChild = actionListChild.nextSibling();
                                    }
                                }
                                functionChild = functionChild.nextSibling();
                            }
                        }
                        functionListChild = functionListChild.nextSibling();
                    }
                }
                modbusChild = modbusChild.nextSibling();
            }
        }
        rootChild = rootChild.nextSibling();
    }
    return true;
}

QList<ModbusControlStruct> ModbusListXml::modbusControlList(const ControlPointStruct &controlPoint)
{
    QList<ModbusControlStruct> modbusControlList;
    if(m_controlMultiMap.contains(controlPoint))
    {
        modbusControlList = m_controlMultiMap.values(controlPoint);
    }
    return modbusControlList;
}

QList<ModbusControlStruct> ModbusListXml::modbusControlList(const QList<ControlPointStruct> &controlPointList)
{
    QList<ModbusControlStruct> modbusControlList;
    foreach (const ControlPointStruct &controlPoint, controlPointList)
    {
        modbusControlList.append(this->modbusControlList(controlPoint));
    }
    return modbusControlList;
}

QList<ControlPointStruct> ModbusListXml::controlPointList(const ModbusControlStruct &modbusControl)
{
    QList<ControlPointStruct> controlPointList;
    if(m_transitionMap.contains(modbusControl))
    {
        QList<RangePointStruct> transitionList = m_transitionMap.value(modbusControl);
        quint16 registerStartAddress = modbusControl.registerStartAddress();

        for(int i=0; i<modbusControl.registrValueList().size(); i++)
        {
            quint16 registerAddress = registerStartAddress + i;
            quint16 registerValue  =  modbusControl.registrValueList().at(i);

            foreach (const RangePointStruct &transition, transitionList)
            {
                if(registerAddress == transition.address())
                {
                    if((registerValue >= transition.valueMin()) && (registerValue <= transition.valueMax()))
                    {
                        controlPointList.append(transition.controlPointList());
                    }
                }
            }
        }
    }

    return controlPointList;
}

QList<quint16> ModbusListXml::deviceAddressList() const
{
    return m_deviceAddressList;
}


bool ModbusListXml::clear()
{
    if(!this->clearAllNode())
    {
        return false;
    }

    m_controlMultiMap.clear();
    m_transitionMap.clear();
    return true;
}


